# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabSchema.types['ExplainVulnerabilityPrompt'], feature_category: :vulnerability_management do
  let_it_be(:source_code) do
    <<~SOURCE
    #include <stdio.h>

    int main(int argc, char *argv[])
    {
      char buf[8];
      memcpy(&buf, "123456789");
      printf("hello, world!");
    }
    SOURCE
  end

  let(:graphql_response) { GitlabSchema.execute(query, context: { current_user: user }).as_json }

  let_it_be(:project) do
    create(:project, :custom_repo, files: {
      'src/main.c' => source_code
    })
  end

  let_it_be(:vulnerability_finding) do
    create(:vulnerabilities_finding,
      :sast,
      project: project,
      location: {
        'file' => 'src/main.c',
        'start_line' => 5,
        'end_line' => 6
      }
    )
  end

  let_it_be(:vulnerability) do
    create(:vulnerability, findings: [vulnerability_finding], project: project)
  end

  let_it_be(:fields) { %i[promptWithCode promptWithoutCode presubmissionChecks] }

  let_it_be(:user) { create(:user, security_dashboard_projects: [project]).tap { |user| project.add_developer(user) } }
  let(:query) do
    gid = vulnerability.to_gid
    %(
        query {
          explainVulnerabilityPrompt(vulnerabilityId: \"#{gid}\") {
            #{fields.join("\n")}
          }
        }
      )
  end

  before do
    stub_licensed_features(security_dashboard: true)
  end

  specify { expect(described_class.graphql_name).to eq('ExplainVulnerabilityPrompt') }

  it { expect(described_class).to have_graphql_fields(fields).only }
end
